#include "MYhardware.h"

extern uint16_t ADC_Value;

#define ADC_VALUE_LIMIT 4
#define ADC_VALUE_MAX 3900
#define ADC_VALUE_MID 2600
#define ADC_VALUE_MIN 1300
#define Det(a, b) (a > b) ? (a - b) : (b - a)

void Delay_us(uint32_t nus)
{
    uint32_t Delay = nus * 72 / 4;
    do
    {
        __NOP();
    } while (Delay--);
}

void Beep(uint32_t ms)
{
    HAL_GPIO_TogglePin(BEEP_GPIO_Port, BEEP_Pin); //
    HAL_Delay(ms);
    HAL_GPIO_TogglePin(BEEP_GPIO_Port, BEEP_Pin);
}

int KeyScan(int click)
{
    uint8_t KeyNum = 0;
    static uint16_t ADC_Last = 0, ADC_Now = 0;
    ADC_Now = ADC_Value;
    if (Det(ADC_Last, ADC_Now) < ADC_VALUE_LIMIT)
    {
        if (ADC_Now > ADC_VALUE_MAX)
        {
            KeyNum = 3;
        }
        else if (ADC_Now > ADC_VALUE_MID)
        {
            KeyNum = 2;
        }
        else if (ADC_Now > ADC_VALUE_MIN)
        {
            KeyNum = 1;
        }
    }
    ADC_Last = ADC_Now;
    return KeyNum;
}
